package fr.n7.sma.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import fr.n7.sma.core.Main;
import fr.n7.sma.view.listeners.IterationListener;

public class Player extends JPanel implements IterationListener {

	private static final long serialVersionUID = 8067203122871551452L;

	enum StartPause {START, PAUSE};

	Map<StartPause, ImageIcon> startPauseIcons = new HashMap<StartPause, ImageIcon>();
	Map<StartPause, ImageIcon> stepsPauseIcons = new HashMap<StartPause, ImageIcon>();
	StartPause state = StartPause.START;
	JLabel iterationsLabel = new JLabel("-");
	JButton startPause = new JButton();
	JButton stop = new JButton(new ImageIcon("resources/icons/stop.png"));
	JButton step = new JButton(new ImageIcon("resources/icons/step.png"));
	JButton steps = new JButton(new ImageIcon("resources/icons/steps.png"));
	Main main;

	public Player(Main main){
		this.main = main;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel iterPanel = new JPanel(new GridLayout(1, 1));
		iterPanel.add(iterationsLabel);
		iterationsLabel.setHorizontalAlignment(JLabel.CENTER);
		iterationsLabel.setPreferredSize(iterationsLabel.getMinimumSize());
		iterationsLabel.setMaximumSize(iterationsLabel.getMinimumSize());

		JPanel actionsPanel = new JPanel();
		startPauseIcons.put(StartPause.START, new ImageIcon("resources/icons/start.png"));
		startPauseIcons.put(StartPause.PAUSE, new ImageIcon("resources/icons/pause.png"));
		stepsPauseIcons.put(StartPause.START, new ImageIcon("resources/icons/steps.png"));
		stepsPauseIcons.put(StartPause.PAUSE, new ImageIcon("resources/icons/pause.png"));
		startPause.setIcon(startPauseIcons.get(state));
		actionsPanel.setLayout(new BoxLayout(actionsPanel, BoxLayout.X_AXIS));
		actionsPanel.add(startPause);
//		actionsPanel.add(stop);
		actionsPanel.add(step);
		actionsPanel.add(steps);

		startPause.addActionListener(new StartPauseListener());
		step.addActionListener(new StepListener());
		steps.addActionListener(new StepsListener());
//		stop.addActionListener(new StopListener());

		add(iterPanel);
		add(actionsPanel);

		main.addIterationListener(this);
	}

	private class StartPauseListener implements ActionListener{

		
		public void actionPerformed(ActionEvent ae) {
			switch(state){
			case PAUSE :
				changeState(StartPause.START);
				break;
			case START :
				changeState(StartPause.PAUSE);
				new Thread(new Runnable(){
					public void run(){
						main.play();
					}
				}).start();
				break;
			}
			startPause.setIcon(startPauseIcons.get(state));
		}

	}

	private class StepListener implements ActionListener{

		
		public void actionPerformed(ActionEvent ae) {
			new Thread(new Runnable(){
				public void run(){
					main.doOneStep();
				}
			}).start();
		}

	}

	private class Steps implements Runnable {

		private int a;

		public Steps(int a){
			this.a = a;
		}

		public void run(){
			main.doSteps(a);
		}

	}


	public boolean doSteps(){
		boolean ok = false;
		String erreur = "";
		String input = "";
		while(!ok){
			try{
				input = JOptionPane.showInputDialog(null, erreur+"Entrez le nombre d'itérations à atteindre :");
				if(input == null){
					return false;
				}else{
					int a = Integer.parseInt(input);
					a = a - main.getIterationNumber();
					if(a > 0){
						new Thread(new Steps(a)).start();
						ok = true;
					}
				}
			}catch(NumberFormatException e){
			}
			if(!ok){
				erreur = "\""+input+"\""+" n'est pas un nombre d'itération à atteindre valide.\n";
			}
		}
		return true;
	}

	private void changeState(StartPause state){
		this.state = state;
		steps.setIcon(stepsPauseIcons.get(state));
		startPause.setIcon(startPauseIcons.get(state));
		if(state == StartPause.START){
			new Thread(new Runnable(){
				public void run(){
					main.pause();
				}
			}).start();
		}
	}
	
	private class StepsListener implements ActionListener{

		
		public void actionPerformed(ActionEvent ae) {
			switch(state){
			case PAUSE :
				changeState(StartPause.START);
				break;
			case START :
				if(doSteps()){
					changeState(StartPause.PAUSE);
				}
				break;
			}
		}

	}

//	private class StopListener implements ActionListener{

//	
//	public void actionPerformed(ActionEvent ae) {
//	state = StartPause.START;
//	startPause.setIcon(icons.get(state));
//	new Thread(new Runnable(){
//	public void run(){
//	main.stop();
//	}
//	}).start();
//	}

//	}

	
	public void updateIteration(int count) {
		iterationsLabel.setText(""+count);
	}

	
	public void updatePause(boolean isPaused) {
		changeState(isPaused?StartPause.PAUSE:StartPause.START);
	}

}
